using System;
using System.ComponentModel;

namespace AntEater.ComponentModel
{
    /// <summary>
    /// Represents a solution.
    /// </summary>
    [Serializable]
    public class Solution : INotifyPropertyChanged
    {
        /// <summary>
        /// Occurs when the instance or some of his child objects has changed.
        /// </summary>
        public event EventHandler<EventArgs> Changed;

        /// <summary>
        /// Occurs when a property of the instance has changed
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Initializes a new instance of the <see cref="Solution"/> class.
        /// </summary>
        public Solution() {
            this._project = new Project();
            this._project.Changed += new EventHandler<EventArgs>(this.ProjectChanged);

            this._buildFiles = new BuildFileCollection();
            this._buildFiles.Changed += new EventHandler<EventArgs>(this.BuildFilesChanged);
        }

        private Project _project;

        /// <summary>
        /// Gets or sets the project.
        /// </summary>
        /// <value>The project.</value>
        public Project Project {
            get { return _project; }
        }

        private BuildFileCollection _buildFiles;

        /// <summary>
        /// Gets the build files.
        /// </summary>
        /// <value>The build files.</value>
        public BuildFileCollection BuildFiles {
            get { return _buildFiles; }
        }

        private bool _isOpen;

        /// <summary>
        /// Gets a value indicating whether this instance is open.
        /// </summary>
        /// <value><c>true</c> if this instance is open; otherwise, <c>false</c>.</value>
        public bool IsOpen {
            get { return _isOpen; }
        }

        /// <summary>
        /// Opens this instance.
        /// </summary>
        public void Open() {
            _project.Open();
            _isOpen = true;
        }

        /// <summary>
        /// Closes this instance.
        /// </summary>
        public void Close() {
            // clear all collections
            _project.Close();
            _buildFiles.Clear();
            _isOpen = false;
        }

        /// <summary>
        /// Projects the changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ProjectChanged(object sender, EventArgs e) {
            this.OnChanged();
        }
        
        /// <summary>
        /// Builds the files changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BuildFilesChanged(object sender, EventArgs e) {
            this.OnChanged();
        }

        /// <summary>
        /// Called when [changed].
        /// </summary>
        protected void OnChanged() {
            if (this.Changed != null) {
                this.Changed(this, new EventArgs());
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName) {
            if (this.PropertyChanged != null) {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

            this.OnChanged();
        }
    }
}
